package com.dbq.fast.business.me;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.util.RudenessScreenHelper;
import com.dbq.fast.util.SystemUtil;


public class CopyWidget {

    private static PopupWindow createPopupWindow(Context context, String text) {
        View popupView = LayoutInflater.from(context).inflate(R.layout.copy, null);

        PopupWindow mPopupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null));

        mPopupWindow.getContentView().setFocusableInTouchMode(true);
        mPopupWindow.getContentView().setFocusable(true);

        popupView.findViewById(R.id.copy_area).setOnClickListener(v -> {
            Toast.makeText(App.getContext(), App.getContext().getString(R.string.tip_copy_success), Toast.LENGTH_SHORT).show();
            SystemUtil.copyToBoad(text);
            mPopupWindow.dismiss();
        });

        return mPopupWindow;
    }

    public static void show(Context context, View anchorView, String text){
        PopupWindow popupWindow = createPopupWindow(context, text);

        int w = (int) (anchorView.getWidth()/2 - RudenessScreenHelper.pt2px(context, 66)/2);
        int h = (int) (- anchorView.getHeight() - RudenessScreenHelper.pt2px(context, 42));

        popupWindow.showAsDropDown(anchorView, w, h);
    }

}
